<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/token.jsp"%>
<%
    Integer uid = token.getUid();
    List<UserProfile> userProfiles = QBuilder.db("basedb").select().from("UserProfile").where(new Where("hospital=", hospital).and("status=", STATUS_ENABLE).and("uid=", uid)).query().getResultObj();
    String uids = "" + uid;
    for (int i = 0; i < userProfiles.size(); i++) {
        UserProfile userProfile = userProfiles.get(i);
        uids += "," + userProfile.getId();
    }
    List<Map> orders = QBuilder.db("basedb").select("new map(id as register_id, patientName as patient_name, doctorName as doctor_name, deptName as dept_name, date as date, half as half, registerStatus as register_status, branch as branch, jsonString as json)").from("RegisterOrder").where(new Where("status=", STATUS_ENABLE).and("userProfileId in("+ uids +")" )).order("date", QBuilder.desc).query().getResultObj();
    if (isEmpty(orders)) {
        msg = new Message(30041, "暂无预约信息");
        msg.put("data", new ArrayList());
        Tools.encryptPrint(out, msg);
        return;
    }
    Map<String, List<Map>> tempMap = new HashMap<String, List<Map>>();
    tempMap.put("本部", new ArrayList());
    tempMap.put("东区", new ArrayList());
    tempMap.put("北区", new ArrayList());
    for (int i = 0; i < orders.size(); i++) {
        Map map = orders.get(i);
        String branch = Tools.getDesc(map.get("branch"), "1");
        String branchName = "1".equals(branch) ? "本部" : "2".equals(branch) ? "东区" : "北区";
        String json = Tools.getDesc(map.remove("json"), "");
        Map<String, String> _map = JSONUtil.fromJson(json, new TypeReference<Map<String, String>>(){});
        map.put("start_time", _map.get("start_time"));
        map.put("end_time", _map.get("end_time"));
        map.put("branch_name", branchName);
        List<Map> tempList = tempMap.get(branchName);
        if (tempList == null) {
            tempList = new ArrayList();
            tempMap.put(branchName, tempList);
        }
        tempList.add(map);
    }
    Set<Entry<String, List<Map>>> set = tempMap.entrySet();
    List<Map> result = new ArrayList<Map>();
    for (Entry<String, List<Map>> entry : set) {
        String key = entry.getKey();
        List<Map> values = entry.getValue();
        Map map = new HashMap();
        map.put("branch_name", key);
        map.put("result", values);
        result.add(map);
    }
    msg.put("data", result);
    Tools.encryptPrint(out, msg);
%>
